﻿using GameEngine.Clients.Silverlight;
using GameEngine.Clients.Silverlight.View.Components;
using GameEngine.Model.Data;
using MySettlers.View.Components;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using System.Windows;
using MySettlers.View;
using MySettlers.Game.View.Components;

namespace MySettlers.Commands
{
    public class Page_Game_In_Command : SimpleCommand
    {
        override public void Execute(INotification notification)
        {
            IGameVO<int> game = notification.Body as IGameVO<int>;
            //MyFacade.Instance.AddGameDefaultData(game);
            var gameDP = SilverlightApplicationFacadeHelper.CreateGame(game);//regista o game mediator etc
            SilverlightApplicationFacadeHelper.StartLevel(game.LevelOrder[game.CurrentLevel]);
            
            //TODO pensar nisto: setlevelview( ,list<worldviews));
            //WorldUC worldView = SilverlightViewFacade<GameUC, LevelUC>.Helper.GetWorldView(new WorldVOId("MyWorld"));

            var worldId = gameDP.Levels[game.LevelOrder[game.CurrentLevel]].Worlds[0].Id;
            MyFacade.Instance.currentWorldId = worldId;
            //var worldMapId = game.Levels[game.LevelOrder[game.CurrentLevel]].Worlds[1].Id;
            //MyFacade.Instance.currentWorldMiniMap = worldMapId;
            var worldView = SilverlightViewFacade.Helper.GetWorldView(worldId);

            var worldDP = MyModel.Helper.GetWorldDataProxy(worldId);
            var mapMed = SilverlightViewFacade.Helper.GetMapMediator(worldDP.Map);

            mapMed.MouseOnItems(true);

            var gameView = ((MyGameUC)MyViewFacade.Helper.GetGameView());

            var levelDP = MyModel.Helper.GetLevelDataProxy(game.LevelOrder[game.CurrentLevel]);
            var levelMediator = MyViewFacade.Helper.GetLevelMediator(game.CurrentLevel) as MyLevelMediator;

            var levelMenuView = new LevelMenuUC();
            var levelMenuMediator = new LevelMenuMediator(levelMenuView,"",levelDP);
            Facade.RegisterMediator(levelMenuMediator);

            var buildingMenuView = new BuildingMenuUC();
            var buildingMenuMediator = new BuildingMenuMediator(buildingMenuView, "");
            Facade.RegisterMediator(buildingMenuMediator);

            var peasantMenuView = new PeasantMenuUC();
            var peasantMenuMediator = new PeasantMenuMediator(peasantMenuView, "");
            Facade.RegisterMediator(peasantMenuMediator);

            levelMediator.setMenu(levelMenuView);

            MyFacadeHelper.DesktopNavigate(MyViewFacade.Helper.GetGameView() as FrameworkElement);
        }
    }

    public class Page_Game_Out_Command : SimpleCommand
    {
        override public void Execute(INotification notification)
        {
            Facade.RemoveMediator(LevelMenuMediator.MYNAME);
            Facade.RemoveMediator(BuildingMenuMediator.MYNAME);
            Facade.RemoveMediator(PeasantMenuMediator.MYNAME);

            SilverlightApplicationFacadeHelper.DestroyGame();
            SendNotification(MyFacade.PAGE_GAMEMAINMENU_IN_COMMAND);
        }
    }
}
